import com.sun.org.apache.xpath.internal.operations.Bool;

import java.util.ArrayList;
import java.util.List;

/**
 * sns
 * Created by Liufeng on 2014/12/6.
 */
public class Sns {
    public static void main(String[] args) {
        int childNum = 16;
        Sns sns = new Sns();
        int i = 1;
        {
            List<Child> list = new ArrayList<Child>();
            while (!(list.size() == 1 && list.get(0).getName().equals("A"))) {
                list = sns.createChile(childNum);
                System.out.println("从第" + i + "个孩子开始报数");
                int j = 1;
                list = sns.getNum(list, i, 3, j);//报数
                list = sns.clearChild(list, 3);//清理
                if (list.size() == 0) {
                    System.out.println("error");
                    return;
                }
                if (++i == childNum + 1) {
                    return;
                }
            }
            if (list.size() == 1) {
                System.out.println("从第" + (i-1) + "个孩子开始报数,最后一个是" + list.get(0).getName());
            }
        }

    }

    /**
     * 创建孩子集合
     *
     * @param num 创建多少个孩子
     * @return list
     */
    public List<Child> createChile(int num) {
        if (num > 24 || num < 0) {
            System.out.println("创建数目无法满足命名需求");
            return null;
        }
        List<Child> list = new ArrayList<Child>(num);
        char i = 65;
        int j = 1;
        while (j <= num) {
            Child child = new Child();
            child.setName(String.valueOf(i));
            list.add(child);
            i++;
            j++;
        }
        System.out.println("总共创建了" + (j - 1) + "个孩子");
        return list;
    }

    /**
     * 孩子报数方法
     *
     * @param children 孩子的集合
     * @param start    从第几个孩子开始报数
     * @param size     报数循环的最大值,1.2.3.1.2.3的话 size为3
     * @param num      第一个孩子报数多少
     */
    public List<Child> getNum(List<Child> children, int start, int size, int num) {
        int j = start;
        boolean cycle = Boolean.FALSE;
        int sizeNum = 0;//报数为3的孩子的数目
        while (cycle == Boolean.FALSE ? Boolean.TRUE : j < start) {
            if (num == size + 1) {
                num = 1;
            }
            if (children.get(j - 1).getNum() != size) {
                children.get(j - 1).setNum(num);
                System.out.println("孩子" + children.get(j - 1).getName() + "报数为" + num);
                num++;
                j++;
            } else {
                j++;
            }
            if(children.get(j-2).getNum() == 3){
                sizeNum++;
            }
            if (j == children.size() + 1) {
                j = 1;
                cycle = Boolean.TRUE;
            }
        }
        int index = 2;
        do{
            int p = (j - index >= 0 ? j - index : children.size() + j - index);
            num = children.get(p).getNum() + 1;
            index++;
        }while(num == 4);
        if (sizeNum != children.size() - 1) {
            getNum(children, start, size, num);
        }
        return children;
    }

    /**
     * 清除不符合要求的孩子
     *
     * @param list 孩子报数后的集合
     * @param base 被清除的依据,报到3的被清除,base就是3
     * @return 一次清理所有符合的孩子的集合
     */
    public List<Child> clearChild(List<Child> list, int base) {
        List<Child> resultList = new ArrayList<Child>();
        for (Child child : list) {
            if (child.getNum() != base && child.getNum() > 0) {
                resultList.add(child);
            }
        }
        list = null;
        return resultList;
    }
}

class Child {
    int num;
    String name;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
